package com.hzbank.juc.share;

import javax.swing.*;

/**
 * @description:
 * @author: libugu@cskaoyan.com
 * @date 2023/12/25 - 10:52
 */
public class PrintABC {

    private final static Object lock = new Object();

    private static boolean aEx = false;
    private static boolean bEx = false;

    private static void printA(){

        synchronized (lock){
            System.out.println("A");
            aEx = true;
            lock.notifyAll();
        }
    }

    private static void printB() throws InterruptedException {

        synchronized (lock) {
            while (!aEx) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
                System.out.println("B");
                bEx = true;
                lock.notifyAll();
            }
        }


    private static void printC() throws InterruptedException {

       synchronized (lock){
           while (!bEx){
               lock.wait();
           }
           System.out.println("C");
       }
    }


    public static void main(String[] args) {
        new Thread(() -> {
            PrintABC.printA();
        }, "A").start();
        //线程B
        new Thread(() -> {
            try {
                PrintABC.printB();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "B").start();
        //线程C
        new Thread(() -> {
            try {
                PrintABC.printC();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "C").start();

    }

}
